package com.yhn.bubblevideo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhn.bubblevideo.admin.service.CategoryService;
import com.yhn.bubblevideo.common.model.BaseResponse;
import com.yhn.bubblevideo.common.model.ResultUtils;
import com.yhn.bubblevideo.model.dto.req.category.CategoryAddRequestDTO;
import com.yhn.bubblevideo.model.dto.req.category.CategoryEditRequestDTO;
import com.yhn.bubblevideo.model.dto.req.category.CategoryQueryRequestDTO;
import com.yhn.bubblevideo.model.dto.req.common.DeleteRequest;
import com.yhn.bubblevideo.model.dto.resp.CategoryRespDTO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yhn
 * @version 1.0
 * @description
 * @date 2024/11/24 15:19
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 列表,分页
     */

    @PostMapping("/list")
    @SaCheckLogin
    public BaseResponse<Page<CategoryRespDTO>> listCategoryPage(@RequestBody @Valid CategoryQueryRequestDTO requestParam) {
        Page<CategoryRespDTO> page = categoryService.queryPage(requestParam);
        return ResultUtils.success(page);
    }


    /**
     * 获取单个信息
     */
    @GetMapping("/info/{id}")
    public BaseResponse<CategoryRespDTO> getCategoryInfoById(@PathVariable("id") Long id) {
        CategoryRespDTO category = categoryService.getInfoById(id);
        return ResultUtils.success(category);
    }


    /**
     * 保存
     */
    @PostMapping("/add")
    public BaseResponse<Long> saveCategory(@RequestBody @Valid CategoryAddRequestDTO requestParam) {
        Long newId = categoryService.saveCategory(requestParam);
        return ResultUtils.success(newId);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateCategory(@RequestBody @Valid CategoryEditRequestDTO requestParam) {
        boolean result = categoryService.updateCategory(requestParam);
        return ResultUtils.success(result);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteCategory(@RequestBody DeleteRequest deleteRequest) {
        boolean result = categoryService.deleteById(deleteRequest);
        return ResultUtils.success(result);
    }


    /**
     * 根据父级分类ID获取子分类
     */
    @GetMapping("/children")
    public BaseResponse<List<CategoryRespDTO>> getChildrenByParentId(@RequestParam("parentCategoryId") Long parentCategoryId) {
        return ResultUtils.success(categoryService.getChildCategory(parentCategoryId));
    }

}
